/*
  Example 14.2
  Using Xbee with Arduino for two-way transmission
  tronixstuff.com/tutorials - Chapter 14
  CC by-sa v2.5 Au
*/

int c=0;
float voltage=0;
float sensor=0;
float celsius=0;
float fahrenheit=0;
float photocell=0;

void sendTemp()
{
  sensor=analogRead(0);
  voltage=((sensor*5000)/1024);
  voltage=voltage-500;
  celsius=voltage/10;
  fahrenheit=((celsius*1.8)+32);
  Serial.println("========================");
  Serial.print("Temperature: ");
  Serial.print(celsius,2);
  Serial.println(" degrees C");
  Serial.print("Temperature: ");
  Serial.print(fahrenheit,2);
  Serial.println(" degrees F");
  Serial.println("========================");
}

void sendLight()
{
  photocell=analogRead(5);
  Serial.println("========================");
  Serial.print("Light reading ");
  Serial.println(photocell, 2);
  Serial.println("========================");
}

 int getserialnumber() // copied from exercise 8.2
  // function to get an integer from the serial monitor box, i.e. user input
  {
    int result=0;
    int q = 0;
    while (Serial.available()==0)
    {
      // do nothing until something comes into the serial buffer
    } 
    while (Serial.available()>0)
    {
      while (Serial.available()>0)
      {
        result=result*10; // move the previous digit to the next column on the left, e.g. 1 becomes 10
        // while there is data in the buffer
        q = Serial.read()-48; // read the next number in the buffer, subtract 48 to convert to the actual number
        result=result+q;
      }
      delay(5); // the processor needs a moment to process
    }
    return result;
  }


void setup()
{
  Serial.begin(9600);
}

void loop()
{
    c=getserialnumber();
    if (c==1)
    {
      sendLight();
    }
    if (c==2)
    {
      sendTemp();
    }
}
